﻿using System;
using System.Collections.Generic;
using System.Linq;
using Model.Entity;
using Ones.Dal.Infrastructure;
using Ones.Dal.Repository.Impl;
using Ones.Infrastructure.Utils.Pagination.Impl;

namespace Ones.Wcf.WebAdministration
{
    public class ServiceAdvertisementManagement : IServiceAdvertisementManagement
    {
        private IAdvertisementBlockDao _daoAdvBlk;
        private IAdvertisementContactDao _daoAdvCont;
        private IAdvertisementDao _daoAdv;
        private IViewAdvertisementDetailDao _daoAdvDetail;

        #region Constructor
        
        public ServiceAdvertisementManagement()
        {
            _daoAdvBlk = new AdvertisementBlockDao();
            _daoAdvCont = new AdvertisementContactDao();
            _daoAdv = new AdvertisementDao();
            _daoAdvDetail = new ViewAdvertisementDetailDao();
        }
        
        #endregion
        
        #region Advertisement Block
        
        #region DMM
        
        public bool
        addAdvertisementBlock(AdvertisementBlock advertisementBlock)
        {
            return saveAdsBlock(advertisementBlock, true);
        }
        
        public bool
        updateAdvertisementBlock(AdvertisementBlock advertisementBlock)
        {
            return saveAdsBlock(advertisementBlock, false);
        }
        
        public bool
        deleteAdvertisementBlock(string blkId)
        {
            return _daoAdvBlk.delete(blkId);
        }
        
        #endregion
        
        #region DQM
        
        public List<AdvertisementBlock>
        searchAllAdvertisementBlocks()
        {
            return _daoAdvBlk.getAll();
        }
        
        public AdvertisementBlock
        searchAdvertisementBlockById(string blkId)
        {
            return _daoAdvBlk.getById(blkId);
        }
        
        public List<AdvertisementBlock>
        searchAdvertisementBlockByActive(bool isActive)
        {
            return _daoAdvBlk.getByActive(isActive);
        }
        
        #endregion
        
        #endregion Advertisement Block
        
        #region Advertisement Contact
        
        #region DMM
        
        public bool
        addAdvertisementContact(AdvertisementContact advertisementContact)
        {
            return saveAdsContact(advertisementContact, true);
        }
        
        public bool
        updateAdvertisementContact(AdvertisementContact advertisementContact)
        {
            return saveAdsContact(advertisementContact, false);
        }
        
        public bool
        deteleAdvertisementContact(int advContId)
        {
            return _daoAdvCont.delete(advContId);
        }
        
        #endregion
        
        #region DQM
        
        public List<AdvertisementContact>
        searchAllAdvertisementContacts()
        {
            return _daoAdvCont.getAll();
        }
        
        public AdvertisementContact
        searchAdvertisementContactById(int advContId)
        {
            return _daoAdvCont.getById(advContId);
        }
        
        public List<AdvertisementContact>
        searchAdvertisementContactByName(string advContName)
        {
            return _daoAdvCont.getByName(advContName);
        }
        
        public List<AdvertisementContact>
        searchAdvertisementContactByCompany(string advContCompany)
        {
            return _daoAdvCont.getByCompany(advContCompany);
        }
        
        #endregion
        
        #endregion Advertisement Contact
        
        #region Advertisement
        
        #region DMM
        
        public bool
        addAdvertisment(Advertisement advertisement)
        {
            DateTime d = DateTime.Now;
            advertisement.advDateCreate = d;
            advertisement.advDateModify = d;
            
            return saveAds(advertisement, true);
        }
        
        public bool
        updateAdvertisement(Advertisement advertisement)
        {
            advertisement.advDateModify = DateTime.Now;
            
            return saveAds(advertisement, false);
        }
        
        public bool
        deteleAdvertisement(int advId)
        {
            return _daoAdv.delete(advId);
        }
        
        #endregion
        
        #region DQM
        
        public List<Advertisement>
        searchAllAdvertisements()
        {
            return _daoAdv.getAll();
        }
        
        public Advertisement
        searchAdvertisementById(int advId)
        {
            return _daoAdv.getById(advId);
        }
        
        public List<Advertisement>
        searchAdvertisementByContact(int advContId)
        {
            return _daoAdv.getByContact(advContId);
        }
        
        public List<Advertisement>
        searchAdvertisementByBlock(string blkId)
        {
            return _daoAdv.getByBlock(blkId);
        }
        
        #endregion
        
        #endregion Advertisement
        
        #region View Advertisement Detail
        
        public List<ViewAdvertisementDetail>
        searchAllAdvertisementDetails()
        {
            return _daoAdvDetail.getAll();
        }
        
        #endregion View Advertisement Detail
        
        #region ///Paging///
        
        public PagingObject<Advertisement>
        getAdvertisement(int page)
        {
            try
            {
                //??? fixed value ?
                int numberOfObjectsPerPage = 10;
                
                var q = _daoAdv.getAll();
                PagingObject<Advertisement> obj = new PagingObject<Advertisement>();
                
                obj.Data = q.Skip(numberOfObjectsPerPage * (page - 1))
                            .Take(numberOfObjectsPerPage)
                            .ToList();
                
                obj.TotalPage = q.Count() / numberOfObjectsPerPage;
                
                if (q.Count() % numberOfObjectsPerPage > 0)
                {
                    obj.TotalPage = obj.TotalPage + 1;
                }
                
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public PagingObject<Advertisement>
        searchAdvertisement(string searchBy, string key, int page)
        {
            try
            {
                //??? fixed value ?
                int numberOfObjectsPerPage = 10;
                int totalRecord = 0;
                
                PagingObject<Advertisement> obj = new PagingObject<Advertisement>();
                
                if (searchBy == "id")
                {
                    var q = _daoAdv.getMultiple(e => e.advContId.ToString().Contains(key) || e.advBlockId.ToString().Contains(key));
                    //hinh nhu int ko xai contains ddc m oi :))
                    //trong Db t cung ko bik VY no xai kieu int lun a :v vai~
                    var queryResultPage = q.Skip(numberOfObjectsPerPage * (page - 1)).Take(numberOfObjectsPerPage);
                    obj.Data = queryResultPage.ToList();
                    totalRecord = q.Count();
                }
                //??? co van de, BUG
                obj.TotalPage = totalRecord / numberOfObjectsPerPage;
                if (totalRecord % numberOfObjectsPerPage > 0)
                    obj.TotalPage = obj.TotalPage + 1;
                return obj;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public LazyPagination<AdvertisementBlock>
        pagingAllAdvertisementBlocks(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoAdvBlk.getAll().AsQueryable();
                return new LazyPagination<AdvertisementBlock>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public LazyPagination<AdvertisementContact>
        pagingAllAdvertisementContacts(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoAdvCont.getAll().AsQueryable();
                return new LazyPagination<AdvertisementContact>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public LazyPagination<Advertisement>
        pagingAllAdvertisements(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoAdv.getAll().AsQueryable();
                var rslt = new LazyPagination<Advertisement>(lst, pageNumber, pageSize);
                return rslt;
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        public LazyPagination<ViewAdvertisementDetail>
        pagingAllAdvertisementDetails(int pageNumber, int pageSize)
        {
            try
            {
                var lst = _daoAdvDetail.getAll().AsQueryable();
                return new LazyPagination<ViewAdvertisementDetail>(lst, pageNumber, pageSize);
            }
            catch (Exception)
            {
                return null;
            }
        }
        
        #endregion
        
        private bool
        saveAdsBlock(AdvertisementBlock adsBlock, bool isCreate)
        {
            if (isCreate)
            {
                return _daoAdvBlk.create(adsBlock);
            }
            return _daoAdvBlk.update(adsBlock);
        }
        
        private bool
        saveAdsContact(AdvertisementContact adsContact, bool isCreate)
        {
            if (isCreate)
            {
                return _daoAdvCont.create(adsContact);
            }
            return _daoAdvCont.update(adsContact);
        }
        
        private bool
        saveAds(Advertisement ads, bool isCreate)
        {
            DateTime dateTime = DateTime.UtcNow;
            
            if (isCreate)
            {
                ads.advDateCreate = dateTime.Date;
                ads.advDateModify = dateTime.Date;
                return _daoAdv.create(ads);
            }
            else
            {
                ads.advDateModify = dateTime.Date;
                return _daoAdv.update(ads);
            }
        }
        //public LazyPagination<ViewAdvertisementDetail>
        //paging(string keyword, int pageNumber, int pageSize)
        //{
        //    try
        //    {
        //        var lst = _daoAdvDetail.getAll().AsQueryable();
        //        return new LazyPagination<ViewAdvertisementDetail>(lst, pageNumber, pageSize);
        //    }
        //    catch (Exception)
        //    {
        //        return null;
        //    }
        //}
    }
}